<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <sxs-input type='text' title='用户名' v-model='username'></sxs-input>
        <sxs-input type='number' title='年龄' v-model='age'></sxs-input>
        <sxs-input type='password' title='密码' v-model='password'></sxs-input>

        <sxs-input2 v-model='username2' :age.sync='age2'></sxs-input2>

        <button @click="submit">提交</button>
        <button @click="reset">重置</button>
    </div>

    <script>
        Vue.component('SxsInput', {
            template: `
                <div>
                    <label>{{title}}</label>
                    <input :type='type' :value='value' @input='handleInput' />
                </div>
            `,
            props: ['type', 'title', 'value'],
            methods: {
                handleInput(e) {
                    // console.log(e.target.value)
                    this.$emit('input', e.target.value)
                }
            }
        })

        Vue.component('SxsInput2', {
            props: ['value', 'age'],
            template: `
                <div>
                    姓名：<input type='text' v-model='newValue' />
                    年龄：<input type='number' v-model='newAge' />
                </div>
            `,
            computed: {
                newValue: {
                    get() {
                        return this.value
                    },
                    set(val) {
                        this.$emit("input", val)
                    }
                },
                newAge: {
                    get() {
                        return this.age
                    },
                    set(val) {
                        this.$emit('update:age', val)
                    }
                }
            }
        })

        new Vue({
            el: '#app',
            data() {
                return {
                    username: 'sxs',
                    age: 22,
                    password: '000000',


                    username2: '孙小双',
                    age2: 33
                }
            },
            methods: {
                submit() {
                    console.log(this.username, this.age, this.password, this.username2, this.age2)
                },
                reset() {
                    this.username = 'sxs'
                    this.age = 22
                    this.password = '000000'
                }
            }
        })
    </script>
</body>
</html>